package com.example.administrator.tenyuanpackage.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.tenyuanpackage.R;
import com.example.administrator.tenyuanpackage.home.adapter.HomeRecyclerAdapter;
import com.example.administrator.tenyuanpackage.home.bean.PreferentialBean;
import com.example.administrator.tenyuanpackage.home.bean.ResultBean;
import com.example.administrator.tenyuanpackage.home.bean.ResultsGoodsBean;
import com.example.administrator.tenyuanpackage.home.loader.GlideImageLoader;
import com.example.administrator.tenyuanpackage.home.mvp.HomePresentImpl;
import com.example.administrator.tenyuanpackage.home.mvp.IHomeView;
import com.example.administrator.tenyuanpackage.home.view.HomePreferential;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;


public class HomeFragment extends Fragment implements IHomeView{

    private Banner banner;
    private List<Object> imageUrls=new ArrayList<>();
    private List<ResultsGoodsBean.RowsBean> mData=new ArrayList<>();
    private HomePreferential homePre;
    private int page=1;
    private XRecyclerView mRecycler;
    private HomeRecyclerAdapter adapter;
    private HomePresentImpl homePresent;
    private View header;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container,false);
        header = LayoutInflater.from(getContext()).inflate(R.layout.item_header_recycler, null);
        banner = (Banner) header.findViewById(R.id.banner);
        homePre = (HomePreferential) header.findViewById(R.id.home_pre);
        mRecycler = (XRecyclerView) view.findViewById(R.id.home_recycler);
        mRecycler.addHeaderView(header);
        banner.setImageLoader(new GlideImageLoader());
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
//        banner.setBannerAnimation(Transformer.Accordion);
        //设置标题集合（当banner样式有显示title时）
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(4000);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        homePresent = new HomePresentImpl(getContext(),this);
        homePresent.getLoopData();
        homePresent.getPreferentials();
        homePresent.getGoodsData(page);
    }


    @Override
    public void getLoopData(ResultBean loopBean) {

        for (int i = 0; i <loopBean.getAdList().size() ; i++) {
            imageUrls.add(loopBean.getAdList().get(i).getImgUrl());
        }
        banner.setImages(imageUrls);

        banner.start();

    }

    @Override
    public void getPreferntialData(ArrayList<PreferentialBean> preferentials) {
        homePre.setPreferentialData(preferentials);
    }

    @Override
    public void getGoodsData(final ResultsGoodsBean goods) {
      //  mData.clear();
        mData.addAll(goods.getRows());

        final GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycler.setLoadingMoreEnabled(true);
        mRecycler.setLayoutManager(manager);
        adapter = new HomeRecyclerAdapter(getContext(), mData);
        mRecycler.addItemDecoration(new DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL));
        mRecycler.setAdapter(adapter);

        mRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                Log.i(TAG, "onLoadMore: =======================");
                homePresent.push();

            }
        });



//        mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            int lastVisibleItem;
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                Log.i(TAG, "onScrollStateChanged: ========"+ adapter.getItemCount());
//
//                if (newState == RecyclerView.SCROLL_STATE_IDLE &&
//                        lastVisibleItem + 1 == adapter.getItemCount()) {
//
//                    homePresent.push();
//                    adapter.notifyDataSetChanged();
//                }
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//                if (layoutManager instanceof GridLayoutManager) {
//                    int last = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
//                    if (last != -1) {
//                        lastVisibleItem = last;
//                    }
//                }
//            }
//        });
    }

    @Override
    public void onPush(ResultsGoodsBean goods) {
        mData.addAll(goods.getRows());
        adapter.notifyDataSetChanged();
    }
}
